package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PBisai;
import com.wuliaogame.entity.PPlayer;
import com.wuliaogame.entity.PTuihui;
import com.wuliaogame.entity.PUser;
import com.wuliaogame.mapper.PBisaiMapper;
import com.wuliaogame.mapper.PHeroMapper;
import com.wuliaogame.mapper.PUserMapper;
import com.wuliaogame.response.Result;
import com.wuliaogame.service.PBisaiService;
import com.wuliaogame.utils.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-21
 */
@RestController
@RequestMapping("/p-bisai")
public class PBisaiController extends JSONAuthentication {
    @Autowired
    PBisaiMapper bisaiMapper;
    @Autowired
    PHeroMapper pHeroMapper;
    @Autowired
    PUserMapper pUserMapper;
    @Autowired
    PBisaiService pBisaiService;
    @ApiOperation(value = "添加比赛记录")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody PBisai pBisai) throws Exception {
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        pBisai.setCreatTime(timestamp);
        if(bisaiMapper.insert(pBisai)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("添加失败");
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "删除比赛记录")
    @PostMapping("/del")
    public void del(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = bisaiMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找全部比赛记录")
    @PostMapping("/find")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<PBisai> page = new Page<>(current, size);
        Page<PBisai> pages = bisaiMapper.selectPage(page, null);
        List<PBisai> list = pages.getRecords();
        int total = bisaiMapper.selectCount(null);
        for (PBisai a:list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeroId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("bsjl", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有比赛记录");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "查找比赛记录通过用户id")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<PBisai> page = new Page<>(current, size);
        Page<PBisai> pages = bisaiMapper.selectPage(page, wrapper);
        List<PBisai> list = pages.getRecords();
        int total = bisaiMapper.selectCount(wrapper);
        for (PBisai a:list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeroId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("bsjl", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有比赛记录");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "excel表批量导入数据")
    @PostMapping("/excel")
    public void excel(HttpServletRequest request, HttpServletResponse response,@RequestParam("salaryBill") MultipartFile excelFile)throws  Exception {
        String name=excelFile.getOriginalFilename();
        List<PBisai> integers = ExcelUtils.excelToShopIdList(excelFile.getInputStream());
        boolean flag=pBisaiService.saveBatch(integers);
        if(flag){
            Result ok = Result.ok().message("批量导入成功").data("a",integers);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("批量导入失败");
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "计算英雄的出场次数")
    @PostMapping("/findCount")
    public void findCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("hero_id","count(*) AS num").groupBy("hero_id");
        List<PBisai> list = bisaiMapper.selectList( wrapper);
        Map<String,Integer> map=new HashMap();

        for (PBisai a:list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeroId()));
            map.put(a.getPHero().getName(),a.getNum());
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("map", map);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有比赛记录");
        this.WriteJSON(request, response, error);
    }

}

